Strict
Import agfx


Class TScreenFade

	Field fadeTime:Float
	Field fadeOut:Bool
	Field ratio# = 0
	Field active:Bool
	Field counter:Float
	
	Method Start:Void(fadeTime:Float, fadeOut:Bool)
		If active Then Return
		active = True
		Self.fadeTime = fadeTime	
		Self.fadeOut = fadeOut

		If fadeOut Then
			ratio = 1
		Else
			ratio = 0
		End
		counter = 0
	End

	Method Update:Void()
		If Not active Return
		counter += AppSpeed()
		
		CalcRatio()
				
		If counter > fadeTime
			active = False
			If fadeOut			
				APP.currentScreen.PostFadeOut()
			Else
				APP.currentScreen.PostFadeIn()
			End
		End
	End
	
	Method CalcRatio:Void()
		ratio = counter/fadeTime
		If ratio < 0 Then ratio = 0
		If ratio > 1 Then ratio = 1			
		If fadeOut Then
			ratio = 1 - ratio
		End
	End
	
	Method Render:Void()
		If Not active Return
		
		SetAlpha 1 - ratio
		SetColor 0, 0, 0
		DrawRect 0, 0, DISPLAY.VSCREEN_WIDTH, DISPLAY.VSCREEN_HEIGHT
		SetAlpha 1
		SetColor 255, 255, 255
	End
	
End


